<?php
class Admin_Model_Users extends Zend_Db_Table {

    public $_name = 'ha_users';
	public function count($parameter){
        $db = Zend_Db_Table::getDefaultAdapter();
        $where = "(1=1)";
        if($parameter['USERNAME'] !=""){
            $ten=$parameter['USERNAME'];
            $where .= " and U.USERNAME LIKE '%".$ten."%'";
        }
        if($parameter['phamvi']!= "" && $parameter['phamvi'] != "2"){
            $phamvi = $parameter['phamvi'];
            $where .= " and U.STATUS = '$phamvi'";
        }
        $sql = "select count(*) as COUNT from ".$this->_name." U WHERE $where";
        $r = $db->query($sql)->fetch();
        return $r['COUNT'];
    }
    public function SelectAll($parameter,$limit,$offset) {
        $db = Zend_Db_Table::getDefaultAdapter();        
        $where = "(1=1)";
        $param = array();
        if($parameter['USERNAME'] !=""){
            $ten=$parameter['USERNAME'];
            $where .= " and U.USERNAME LIKE '%".$ten."%'";
        }
        if($parameter['phamvi']!= "" && $parameter['phamvi'] != "2"){
            $phamvi = $parameter['phamvi'];
            $where .= " and U.STATUS = '$phamvi'";
        }
        $sql = "select * from ".$this->_name." U WHERE $where limit $offset,$limit ";
        $r = $db->query($sql)->fetchAll();
        return $r;
    }
    public function AddNewUser($Params){
        $id = $Params['id'];        
        $db = Zend_Db_Table::getDefaultAdapter();
        $data = array(
            'USERNAME' => $Params['USERNAME'],
            'FULLNAME' => $Params['FULLNAME'],
            'EMAIL' => $Params['EMAIL'],
            'SOCMND' => $Params['SOCMND'],
            'PHONE' =>$Params['PHONE'],
            'ID_COMPANY' =>$Params['COMPANY'],
            'STATUS' => 0,
            'ROLE' => $Params['ROLE']
        );
        if($Params['PASSWORD'] != ""){
            $data['PASSWORD'] = md5($Params['PASSWORD']);
        }
        if((int)$Params['STATUS'] == 1){
            $data['STATUS'] = 'approved';
        }        
        try {
            if($id ==0){
                $db->insert($this->_name, $data);
            }
            else{
                $db->update($this->_name, $data,"ID_U = ".$id);
            }
        }catch (Zend_Exception $e) {
            echo ($e->getMessage());
            exit;
        }
    }
    public function FindById($id) {
        $result = $this->getDefaultAdapter()->query("
			SELECT
				*
			FROM
				$this->_name
			WHERE
				ID = ?
		", $id);
        return $result->fetch();
    }
    public function findByIdJoinCompany($id) {
        $select = $this->select();
        $where = $this->getAdapter()->quoteInto('ID_U = ?', $id);
        $select->from($this,array("ID_U","USERNAME","EMAIL","SOCMND","FULLNAME","PHONE","ROLE","STATUS","ID_COMPANY"))
               ->join(array("com"=>"company","ha_users.ID_COMPANY = com.id"),array('id as idcompany','name'))
               ->where($where);
        $data = $this->fetchAll($select);
        return $data;
    }
    public function GetListUsers(){
      $sql = "select ID_U,FULLNAME from $this->_name";
      $db = Zend_Db_Table::getDefaultAdapter();
      $r = $db->query($sql);
      return $r->fetchAll();
     }

    public function GetUsersById($id) {
          $sql = "select * from $this->_name where ID_U = '".$id."'";
          $db = Zend_Db_Table::getDefaultAdapter();
          $r = $db->query($sql);
          return $r->fetch();
    }
    public function GetUserByUser($username) {
        $db = Zend_Db_Table::getDefaultAdapter();

        $sql = "select * from $this->_name where username='".$username."'";
        $stmt = $db->query($sql);
        $r = $stmt->fetch();
        if ($r)
            return 1;
        else return 0;
    }
}